package servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import pojo.Admin;
import service.AdminManager;
import service.impl.AdminManagerImpl;

public class LoginServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private AdminManager manager = null;
       
    public LoginServlet() {
        super();
        manager = new AdminManagerImpl();
    }

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String name = request.getParameter("name");
		String pwd = request.getParameter("password");
		String errorMessage = "";
		System.out.println("log..."+name+"...."+pwd);
		Admin admin = manager.findByName(name);
		if(admin!=null&&pwd.equals(admin.getPassword())){
			request.getSession().setAttribute("name", name);
			response.sendRedirect("/admins/merchant/audit/wait");
		}else{
			errorMessage = "用户名或密码错误";
			request.setAttribute("errormsg", errorMessage);
			request.getRequestDispatcher("login.jsp").forward(request, response);
		}
	}

}
